package com.htjc.integral.activity.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.htjc.integral.activity.enums.Constant;
import com.htjc.integral.activity.mapper.EcBpSignInMapper;
import com.htjc.integral.activity.service.IIntegralSignInService;
import com.htjc.integral.activity.util.PageVo;
import com.htjc.integral.activity.util.RedisTem;
import com.htjc.integral.activity.util.ResultModel;
import com.htjc.integral.activity.util.UrlConfigUtil;
import com.htjc.integral.activity.util.enums.RedisKey;
import com.htjc.integral.common.util.RequestUtils;
import com.htjc.integral.common.util.StrUtil;
import com.htjc.integral.common.util.TimeUtil;
import com.htjc.integral.common.util.UUIDGenerator;
import com.htjc.integral.model.activity.EcBpSignNum;
import com.htjc.integral.model.activity.EcBpSignRecord;
import com.htjc.integral.model.activity.EcPromotionParamValueSimple;
import com.htjc.integral.model.activity.EcPromotionSimple;
import com.htjc.integral.model.activity.SignDetail;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "jfLog")
@Service
public class IntegralSignInServiceImpl implements IIntegralSignInService{
	
	@Autowired
	private EcBpSignInMapper ecBpSignInMapper;
	
	@Autowired
	private RedisTem redisTem;
	
	@Autowired
	private UrlConfigUtil urlConfigUtil;
	
	/**
	 * 进行签到接口
	 * @param jsonObject
	 * @return
	 */
	@Transactional
	public JSONObject marchSignIn(JSONObject jsonObject) {
		String ip = jsonObject.getString("IP");//当前请求IP
		String verifyCode = jsonObject.getString("VERIFY_CODE");//1正确0错误
		String unId = jsonObject.getString("UNION_ID");//统一用户ID/账号
		String channel=jsonObject.getString("CHANNEL_NO");//渠道编码
		String userMobile=jsonObject.getString("USER_MOBILE");//手机号
		JSONObject resultJson=new JSONObject();//报错信息返回
//		resultJson.put("RESULT_CODE",Constant.RESULT_CODE_SUCCESS);	
//		resultJson.put("RESULT_MESSAGE",Constant.RESULT_MSG_SUCCESS);
		//判断是否满足签到条件
		if(!checkSignInCondition(ip, verifyCode, resultJson)){
			resultJson.put("IS_SUCCESS",Constant.SIGN_FAIL_CODE);
			return ResultModel.ReturnSucceedJson(resultJson);
		}
		//满足条件，判断是否有活动
		List<EcPromotionSimple> list=new ArrayList<EcPromotionSimple>();
		if(!checkIsActivity(resultJson,list)){
			resultJson.put("IS_SUCCESS",Constant.SIGN_FAIL_CODE);
			return ResultModel.ReturnSucceedJson(resultJson);
		}
		//有活动，进行签到
		signOneDay(ip, unId, list.get(0),channel,userMobile,resultJson);
		return ResultModel.ReturnSucceedJson(resultJson);
	}
	
	/**
	 * 获取签到天数接口
	 * @param jsonObject
	 * @return
	 */
	public JSONObject getSignedDays(JSONObject jsonObject) {
		JSONObject resultJson=new JSONObject();
		String unId = jsonObject.getString("UNION_ID");//统一用户ID/账号
		String MONTH = jsonObject.getString("MONTH");//查询月份
		try {
			// 已签天数
			Map<String, String> params=new HashMap<String, String>();
			params.put("userAccount", unId);
			params.put("month", MONTH);
			List<EcBpSignRecord> recordList = ecBpSignInMapper.selectSignedRecord(params);
			if (recordList != null && !recordList.isEmpty()) {
				List<String> allDays = new ArrayList<String>();
				for (EcBpSignRecord ecBpSignRecord : recordList) {
					if (ecBpSignRecord.getSignDate() != null) {
						allDays.add(TimeUtil.getDateFomat(ecBpSignRecord.getSignDate(),"yyyy-MM-dd"));
						resultJson.put("ALL_DAYS", allDays);
					}
				}
			}
			//连签天数
			EcBpSignNum signNum = ecBpSignInMapper.selectByUserAccount(unId);
			if(signNum!=null){
				resultJson.put("CURRENT_SEQUENT_DAY", signNum.getSequentNum());
				if(betweenDay(signNum.getLastSignDate(),new Date())==0){
					resultJson.put("IS_SIGNED", Constant.SIGN_SUCCESS_CODE);
				}else{
					resultJson.put("IS_SIGNED", Constant.SIGN_FAIL_CODE);
				}
			}
//			resultJson.put("RESULT_CODE",Constant.RESULT_CODE_SUCCESS);
//			resultJson.put("RESULT_MESSAGE",Constant.RESULT_MSG_SUCCESS);
		} catch (Exception e) {
			log.error("获取当月签到天数异常：{}",e);
//			resultJson.put("RESULT_CODE", Constant.RESULT_CODE_FAIL);
//			resultJson.put("RESULT_MESSAGE","系统繁忙，请稍后重试");
			return ResultModel.ReturnErrorJson(resultJson);
		}
		return ResultModel.ReturnSucceedJson(resultJson);
	} 
	
	/**
	 * 获取签到详情接口
	 * @param jsonObject
	 * @return
	 */
	public JSONObject getSignDetail(JSONObject jsonObject) {
		String unId = jsonObject.getString("UNION_ID");//统一用户ID/账号
		long pageSize = jsonObject.getLong("PAGE_SIZE");
		long page = jsonObject.getLong("PAGE");
		JSONObject resultJson=new JSONObject();
		PageVo<SignDetail> pageVo = new PageVo<SignDetail>();
		pageVo.setPAGE_SIZE(pageSize);
		pageVo.setCUR_PAGE(page);
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userAccount", unId);
		params.put("pageSize", pageVo.getPAGE_SIZE());
		params.put("curPosition", pageVo.getSTART());
		params.put("preMonthDate", TimeUtil.getCurrentMonthMinDayDate());
		params.put("currentMonth", TimeUtil.getDate("yyyyMM"));
//		params.put("currentMonth", "201809");
		List<Map<String, Object>> sdList = new ArrayList<Map<String, Object>>();
		int totalSignDays = 0;
		Integer continueSignDays = 0;
		int preMonthSignDays = 0;
		int grandTotalPoints = 0;
		int rowCount = 0;
//		String rtnCode="";
//		String rtnMsg="";
		try {
			if(ecBpSignInMapper.selectUserIsSign(unId)>=1){//有签到记录
				sdList=ecBpSignInMapper.selectSignDetailByUser(params);
				totalSignDays = ecBpSignInMapper.selectSignTotleNum(unId);
				continueSignDays = ecBpSignInMapper.selectSignSequentNum(params);
				preMonthSignDays = ecBpSignInMapper.selectSignPreMonthNum(params);
				grandTotalPoints = ecBpSignInMapper.selectSignPointSum(unId);
				rowCount = ecBpSignInMapper.selectSignDetailCount(unId);
//				rtnCode=Constant.RESULT_CODE_SUCCESS;
//				rtnMsg=Constant.RESULT_MSG_SUCCESS;
			}
		} catch (Exception e) {
//			rtnCode=Constant.RESULT_CODE_FAIL;
//			rtnMsg="获取签到详情异常";
			log.error("获取签到详情异常：{}",e);
			return ResultModel.ReturnErrorJson(resultJson);
		}
		resultJson.put("CONTINUE_SIGN_DAYS", continueSignDays==null?0:continueSignDays);//连续签到天数
		resultJson.put("PRE_MONTH_SIGN_DAYS", preMonthSignDays);//连续签到天数
		resultJson.put("TOTAL_SIGN_DAYS", totalSignDays);//连续签到天数
		resultJson.put("GRAND_TOTAL_POINTS", grandTotalPoints);//连续签到天数
		resultJson.put("PAGE_COUNT", rowCount%pageSize == 0 ?rowCount/pageSize: rowCount/pageSize+1);//连续签到天数
		resultJson.put("CUR_PAGE", page);//连续签到天数
		resultJson.put("START", (page-1) * pageSize);//连续签到天数
		resultJson.put("PAGE_SIZE", pageSize);//连续签到天数
		resultJson.put("ROW_COUNT", rowCount);//连续签到天数
		resultJson.put("RESULT_LIST", sdList);//连续签到天数
		
//		pageVo.setGRAND_TOTAL_POINTS(grandTotalPoints);
//		pageVo.setCONTINUE_SIGN_DAYS(continueSignDays);
//		pageVo.setTOTAL_SIGN_DAYS(totalSignDays);
//		pageVo.setPRE_MONTH_SIGN_DAYS(preMonthSignDays);
//		pageVo.setRESULT_LIST(sdList);
//		pageVo.setROW_COUNT(rowCount);
//		resultJson = (JSONObject)JSONObject.toJSON(pageVo);
//		resultJson.put("RESULT_CODE", rtnCode);
//		resultJson.put("RESULT_MESSAGE", rtnMsg);
		return ResultModel.ReturnSucceedJson(resultJson);
	}
	
	/**
	 * 获取签到规则接口
	 * @param jsonObject
	 * @return
	 */
	public JSONObject getSignRules(JSONObject jsonObject){
		JSONObject resultJson=new JSONObject();
		List<EcPromotionSimple> promotionInfo = ecBpSignInMapper.selectPromotionInfoByDefineId("PROM_INTEGRAL_SIGN");
		if(promotionInfo!=null&&promotionInfo.size()>0){
			Date proStartTime = promotionInfo.get(0).getPromotionStartTime();
			Date proEndTime = promotionInfo.get(0).getPromotionEndTime();
			Date nowDate = new Date();
			if (proStartTime.compareTo(nowDate)==-1&&nowDate.compareTo(proEndTime)==-1) {
				Map<String, String> par = new HashMap<String, String>();
				par.put("promotionId", promotionInfo.get(0).getPromotionId());// 促销活动编号
				par.put("promotionDefineId","PROM_INTEGRAL_SIGN");// 促销工具编号
				List<EcPromotionParamValueSimple> paramValueList = ecBpSignInMapper.selectValueByPromotionIdAndDefineId(par);// 促销工具参数值集合
				if (paramValueList != null && !paramValueList.isEmpty()) {
					resultJson.put("rules", paramValueList);
				}
			}
		}
		resultJson.put("isSuccess",true);
		return resultJson;
	}

	/**
	 * ==================================私有方法==================================
	 */
	
	/**
	 * 判断是否可以进行签到
	 * 1.判断ip是否为黑名单
	 * 2.若10s内连续签到加入黑名单
	 * 3.判断是否需要验证码
	 * @return
	 */
	private boolean checkSignInCondition(String ip,String verifyCode,JSONObject json){
		//判断是否为黑名单IP
//		if(ecBpSignInMapper.selectBlackUser(ip)>0){
//			log.info("【进行签到】该IP{}是黑名单IP",ip);
//			json.put("MSG","系统繁忙，请稍后重试");
//			return false;
//		}
		//判断该IP10秒内是否签到过
//		Date date = new Date();
//	    date.setSeconds(date.getSeconds() - 10);
//	    Map<String, Object> params = new HashMap<String,Object>();
//	    params.put("ip", ip);
//	    params.put("time", date);
//	    if(ecBpSignInMapper.selectTenSTimes(params)>0){
//	    	//加入黑名单IP
//	    	EcBpSignBlackIp ecBpSignBlackIp=new EcBpSignBlackIp();
//	    	ecBpSignBlackIp.setId(UUIDGenerator.getInstance().getReplaceNotMd5UUID());
//	    	ecBpSignBlackIp.setIp(ip);
//	    	ecBpSignBlackIp.setCreatetime(new Date());
//	    	ecBpSignInMapper.insertOneBlackIP(ecBpSignBlackIp);
//	    	log.info("【进行签到】该IP{}10s内签到过，加入黑名单",ip);
//	    	json.put("MSG","系统繁忙，请稍后重试");
//	    	return false;
//	    }
	    //判断是否需要验证码（5次以上需要验证码） 10次以上不能继续签到
	    int signNum = ecBpSignInMapper.selectTodayIP(ip);
	    if(signNum>=10){
	    	log.info("【进行签到】该IP{}已经签到{}次，不能再签到了",ip,signNum);
	    	json.put("MSG", "该IP已经签到" + signNum + "次，不能再签到了");
	    	return false;
	    }else if(signNum>=5&&!"1".equals(verifyCode)){
	    	log.info("【进行签到】该IP{}已经签到{}次，需要验证码",ip,signNum);
	    	json.put("MSG","需要验证码");
	    	return false;
	    }
		return true;
	}
	
	/**
	 * 判断是否有活动
	 * @param list
	 * @return
	 * @throws Exception 
	 */
	private boolean checkIsActivity(JSONObject json,List<EcPromotionSimple> list){
//		list=ecBpSignInMapper.selectPromotionInfoByDefineIdAndTime("PROM_INTEGRAL_SIGN");
		list.addAll(ecBpSignInMapper.selectPromotionInfoByDefineIdAndTime("PROM_INTEGRAL_SIGN"));
		if (list == null||list.size()==0) {
			log.info("【进行签到】该时间段没有活动");
	    	json.put("MSG","该时间段没有活动");
			return false;
	    }
	    int residualIntegral = list.get(0).getResidualIntegral();//剩余积分
	    Date proStartTime = list.get(0).getPromotionStartTime();//开始时间
	    Date proEndTime = list.get(0).getPromotionEndTime();//结束时间
	    Date nowDate = new Date();
	    //判断活动是否开始/结束
	    if (nowDate.compareTo(proStartTime) == -1) {
	    	log.info("【进行签到】签到活动未开始");
	    	json.put("MSG","签到活动未开始");
	        return false;
	    }
	    if (nowDate.compareTo(proEndTime) == 1) {
	    	log.info("【进行签到】签到活动已结束");
	    	json.put("MSG","签到活动已结束");
	        return false;
	    }
	    if (residualIntegral <= 0) {
	    	log.info("【进行签到】签到活动已结束");
	    	json.put("MSG","签到活动已结束");
	        return false;
	    }
		return true;
	}
	
	/**
	 * 开始进行签到
	 * @param ip
	 * @param promotionInfo
	 * @return
	 * @throws Exception 
	 */
	private void signOneDay(String ip,String unId,EcPromotionSimple ecPromotionSimple,String channel,String userMobile,JSONObject resultJson){
		String promotionId=ecPromotionSimple.getPromotionId();
		String currentDate=(String) TimeUtil.getTimeStamp("yyyyMMdd");
		//签到记录只有插入跟查询，不需要乐观锁
//		String key = "VIP_HandleSign_" + promotionId;
//	    Integer version = new Integer(0);
	    String key2 = "VIP_HandleSignNum_" + promotionId;
	    Integer version2 = new Integer(0);
	    //签到活动版本 暂不使用乐观锁
//	    String key3 = "VIP_HandleSignPromotion_" + promotionId;
//	    Integer version3 = new Integer(0);
	    //更新今天的签到记录在redis中的版本
//	    if(!redisTem.exists(key + "_" + unId + "_" + currentDate)) {
//	    	List<EcBpSignRecord> ecBpSignRecordList = ecBpSignInMapper.selectSignedRecordByToday(unId);
//	    	if(ecBpSignRecordList != null && ecBpSignRecordList.size() > 0) {
//				version = ecBpSignRecordList.get(0).getVersion();
//				redisTem.set(key + "_" + unId + "_" + currentDate, version.toString());
//	    	}
//		}else{
//			String tmpVersion = redisTem.get(key + "_" + unId + "_" + currentDate);
//			if(!StrUtil.isEmpty(tmpVersion)) {
//				version = new Integer(tmpVersion);
//			}
//		}
//	    log.info("【进行签到】redis签到记录version:" + version);
	    //更新签到次数在redis中的版本
	    if(!redisTem.exists(key2 + "_" + unId + "_" + currentDate)) {
	    	EcBpSignNum ecBpSignNum = ecBpSignInMapper.selectByUserAccount(unId);
	    	if(ecBpSignNum != null) {
	    		version2 = ecBpSignNum.getVersion();
	    		redisTem.set(key2 + "_" + unId + "_" + currentDate, version2.toString());
	    	}
		}else{
			String tmpVersion = redisTem.get(key2 + "_" + unId + "_" + currentDate);
			if(!StrUtil.isEmpty(tmpVersion)) {
				version2 =  new Integer(tmpVersion);
			}
		}
	    log.info("【进行签到】redis签到次数version:" + version2);
	    //更新签到活动在redis中的版本
//	    if(!redisTem.exists(key3 + "_" + unId + "_" + currentDate)) {
//	    	if(ecPromotionSimple != null) {
//	    		version3 = ecPromotionSimple.getVersion();
//	    		redisTem.set(key3 + "_" + unId+ "_" + currentDate, version3.toString());
//	    	}
//		}else{
//			String tmpVersion = redisTem.get(key3 + "_" + unId + "_" + currentDate);
//			if(!StrUtil.isEmpty(tmpVersion)) {
//				version3 =  new Integer(tmpVersion);
//			}
//		}
//	    log.info("【进行签到】redis签到活动version:"+version3);
	    //查询签到活动规则
	    Map<String, String> params=new HashMap<String, String>();
	    params.put("promotionId", promotionId);
	    params.put("promotionDefineId", "PROM_INTEGRAL_SIGN");
	    List<EcPromotionParamValueSimple> paramValueList = ecBpSignInMapper.selectValueByPromotionIdAndDefineId(params);
	    if (paramValueList == null||paramValueList.size()==0) {
	    	//修改为枚举方式
	    	log.info("【进行签到】未制定签到规则");
	    	resultJson.put("MSG", "未制定签到规则");
	    	resultJson.put("IS_SUCCESS",Constant.SIGN_FAIL_CODE);
	    	return;
        }
	    //制定了签到活动规则，继续签到
	    int i=-1;
	    int nextDayNum = -1;
	    int sequent;
	    int addPoint = 0;
	    int sequentDay = 0;
	    EcBpSignNum ecBpSignNum = ecBpSignInMapper.selectByUserAccount(unId);
	    if(ecBpSignNum==null){
	    	log.info("【进行签到】该账户{}未签到过",unId);
	    	i=0;
	    	nextDayNum=1;
	    }else{
	    	log.info("【进行签到】该账户{}上次签到时间："+TimeUtil.getDateFomat(ecBpSignNum.getLastSignDate(),"yyyy-MM-dd HH:mm:ss"),unId);
			int isContinuous = 0;
			try {
				isContinuous = betweenDay(ecBpSignNum.getLastSignDate(), new Date());
			} catch (Exception e) {
				log.error("【进行签到】判断两个日期间隔天数异常");
				resultJson.put("MSG","系统繁忙，请稍后重试");
				resultJson.put("IS_SUCCESS",Constant.SIGN_FAIL_CODE);
	            return;
			}
			if(isContinuous==0){
				log.error("【进行签到】该账户{}今天已经签到过",unId);
				resultJson.put("MSG", "今天已经签到过");
				resultJson.put("IS_SUCCESS",Constant.SIGN_FAIL_CODE);
	            return;
			}else if(isContinuous==1){
				sequent = ecBpSignNum.getSequentNum().intValue();
                if ((sequent + 1) % 7 == 0)
                	i = 2;
                else {
                	i = 1;
                }
                nextDayNum = ((sequent + 2) % 7 == 0) ? 2 : 1;
                if(isSameMonth(ecBpSignNum.getLastSignDate(), new Date())){
                	sequentDay = ecBpSignNum.getSequentNum().intValue() + 1;
                	ecBpSignNum.setSequentNum(Integer.valueOf(sequentDay));
                }else{
                	ecBpSignNum.setSequentNum(1);
                }
			}else if(isContinuous>1){
				i = 1;
                nextDayNum = 1;
                ecBpSignNum.setSequentNum(1);
			}else{
				log.error("【进行签到】签到日期发生错误");
				resultJson.put("MSG","系统繁忙，请稍后重试");
				resultJson.put("IS_SUCCESS",Constant.SIGN_FAIL_CODE);
	            return;
			}
	    }
	    EcPromotionParamValueSimple selectParamValue = (EcPromotionParamValueSimple)paramValueList.get(i);
        addPoint = Integer.parseInt(selectParamValue.getPromotionParamValue());
        int j = 0;
        //活动剩余积分
        int residualIntegral=ecPromotionSimple.getResidualIntegral();
        if (addPoint > residualIntegral) {
	        addPoint = residualIntegral;
	        j = 0;
        } else if (residualIntegral > addPoint) {
            j = residualIntegral - addPoint;
        }
        Map<String, Object> params2=new HashMap<String, Object>();
        params2.put("residuePoint", Integer.valueOf(j));
        params2.put("addPoint", Integer.valueOf(addPoint));
        params2.put("promotionId", promotionId);
//        params2.put("version", version3);
        if(ecBpSignInMapper.updateResidualIntegral(params2)<=0){
        	log.error("【进行签到】更新签到活动剩余积分异常");
        	resultJson.put("MSG","据上次签到时间过短");
        	resultJson.put("IS_SUCCESS",Constant.SIGN_FAIL_CODE);
            return;
        }
	    if(i==0&&ecBpSignNum==null){
	    	//第一次签到，插入签到次数记录
	    	sequentDay = 1;
	    	EcBpSignNum oneSignNum=new EcBpSignNum();
	    	oneSignNum.setUserAccount(unId);
	    	oneSignNum.setSequentNum(1);
	    	oneSignNum.setTotalNum(1);
	    	oneSignNum.setLastSignDate(new Date());
	    	ecBpSignInMapper.insertOneSignNum(oneSignNum);
	    }else{
	    	//更新签到次数记录
	    	ecBpSignNum.setTotalNum(ecBpSignNum.getTotalNum()+1);
	    	ecBpSignNum.setLastSignDate(new Date());
	    	ecBpSignNum.setVersion(version2);
	    	if(ecBpSignInMapper.updateSignNum(ecBpSignNum)<=0){
	    		log.error("【进行签到】该账户{}更新签到次数异常",unId);
	    		resultJson.put("MSG","据上次签到时间过短");
	    		resultJson.put("IS_SUCCESS",Constant.SIGN_FAIL_CODE);
	            return;
	    	}
	    }
	    //更新redis签到次数版本
	    log.info("【进行签到】该账户{}第{}次签到更新数据库签到次数成功",unId,ecBpSignNum==null?1:ecBpSignNum.getTotalNum());
	    EcBpSignRecord oneSignRecord = new EcBpSignRecord();
	    oneSignRecord.setSignRecordId(UUIDGenerator.getInstance().getReplaceNotMd5UUID());
	    oneSignRecord.setUserAccount(unId);
	    oneSignRecord.setUserMobile(userMobile);
	    oneSignRecord.setSignDate(new Date());
	    oneSignRecord.setSignGivePoint(addPoint);
	    oneSignRecord.setPromotionId(selectParamValue.getPromotionId());
	    oneSignRecord.setPromotionDefineParamId(selectParamValue.getPromotionParamId());
	    oneSignRecord.setIp(ip);
	    oneSignRecord.setOrigin("本次签到来自"+channel+"端");
	    //插入签到积分记录
	    ecBpSignInMapper.insertOneRecord(oneSignRecord);
	    log.info("【进行签到】该账户{}第{}次签到插入数据库签到记录成功",unId,ecBpSignNum==null?1:ecBpSignNum.getTotalNum());
	    redisTem.set(key2 + "_" + unId + "_" + currentDate, ecBpSignNum==null?"1":version2+1+"");
	    log.info("【进行签到】该账户{}第{}次签到更新redis签到次数成功",unId,ecBpSignNum==null?1:ecBpSignNum.getTotalNum());
	    //调用资产中心接口增加账户积分
 		JSONObject json=new JSONObject();
		json.put("LOGIN_ACCOUNT_ID", unId);//登录账户ID
		json.put("BUSINESS_ID", UUIDGenerator.getInstance().getReplaceNotMd5UUID());//业务ID
		json.put("INCOME_SOURCE", "02");//收入来源01：网省积分流入02：统一活动收入03：网省活动收入04：支出失败退回
		json.put("OPERATION_STATUS", "01");//操作类型01：自动02：人工
		json.put("SOURCE_DESCRIBE", "签到赠送积分");//来源说明01：正常02：锁定03：注销 DEFALULT：01
		json.put("INCOME_NUM", addPoint);//收入总金额
		json.put("IP","192.0.0.1");
//		headerMap.put("CHANNEL_RESOURCE",json.getString("CHANNEL_NO"));//渠道编码
		json.put("CHANNEL_RESOURCE","5");//渠道编码
		json.put("USER_NAME","网上国网签到");//
		String ACCOUNT_INTEGRAL_DEDUCTION = urlConfigUtil.getUrlByKey(RedisKey.URLCON_PREFIXX.getKey(), RedisKey.ACCOUNT_INTEGRAL_ADD.getKey());
		log.info("账户中心积分增加接口地址："+ACCOUNT_INTEGRAL_DEDUCTION);
		String sendPost = RequestUtils.sendPost(ACCOUNT_INTEGRAL_DEDUCTION, json.toJSONString());
	    //如果资产中心返回成功
	    if(Constant.RESULT_CODE_SUCCESS.equals(((JSONObject)JSONObject.parse(sendPost)).getString("rcode"))){
	    	log.info("【进行签到】该账户{}第{}次签到成功",unId,ecBpSignNum==null?1:ecBpSignNum.getTotalNum());
	    	resultJson.put("IS_SUCCESS", Constant.SIGN_SUCCESS_CODE);
	    	resultJson.put("SEQUENT_DAY", ecBpSignNum==null?1:ecBpSignNum.getSequentNum());
	    	resultJson.put("MSG", "您已获得"+addPoint+"积分,明日继续签到可获得" + paramValueList.get(nextDayNum).getPromotionParamValue() + "积分");
	    }else{
	    	log.info("【进行签到】该账户{}第{}次签到异常，资产中心返回失败",unId,ecBpSignNum==null?1:ecBpSignNum.getTotalNum());
	    	resultJson.put("IS_SUCCESS", Constant.SIGN_FAIL_CODE);
	    	resultJson.put("MSG", "签到异常，请稍后再试");
	    }
	}
	
	
	/**
	 * 计算两个日期间隔天数
	 * @param sDate
	 * @param bDate
	 * @return
	 * @throws Exception
	 */
	private int betweenDay(Date sDate, Date bDate) throws Exception{
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    sDate = sdf.parse(sdf.format(sDate));
	    bDate = sdf.parse(sdf.format(bDate));
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(sDate);
	    Long sLong = Long.valueOf(cal.getTimeInMillis());
	    cal.setTime(bDate);
	    Long bLong = Long.valueOf(cal.getTimeInMillis());
	    int betweenDay = (int)((bLong.longValue() - sLong.longValue()) / 86400000L);
	    return betweenDay;
	}
	
	/**
	 * 判断两个日期是否为同一个月
	 * @param sDate
	 * @param bDate
	 * @return
	 * @throws Exception
	 */
	private boolean isSameMonth(Date sDate, Date bDate){
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
	    String s = sdf.format(sDate);
		String b =sdf.format(bDate);
	    if(s.equals(b)){
	    	return true;
	    }
	    return false;
	}
	
}
